#ifndef __HEIGHT_FEILD_H__
#define __HEIGHT_FEILD_H__

//Include Utility for get some variable in Opengl 
#include "../Utilities/utilities.h"
#include "TerrainConfig.h"

class HeightField
{
private:
	char *m_fileSrcName; //Name of source file
public:
	int Cols; //Number of column in map data
	int Rows; //Number of row in map data
	GLfloat *HeightData; //List store height of map
private:

	///Function is open and load base source in file source data
	//Input parameter:
	//	fileSrcName: Source file name
	//Output:
	//	List of variable
	GLubyte* OpenSourceFile(char *fileSrcName, long &size);

	///Function is convert data color 8-bit to height value
	//Input parameter:
	//	Src: Array of value will be converted, each value is 1 byte, it is color data with structure
	//	8-Bit color: R:3, G:3, B:2
	//	Des: Height map data receive
	//Output:
	//	Void
	void ReadHeightFromColor8Bit(GLfloat *Des, GLubyte *Src);

	///Function is convert data color 16-bit to height value
	//Input parameter:
	//	Src: Array of value will be converted, each value is 2 byte, it is color data with structure
	//	16-Bit color: R:5, G:6, B:5
	//	Des: Height map data receive
	//Output:
	//	Void
	void ReadHeightFromColor16Bit(GLfloat *Des, GLushort *Src);

	///Function check a position for get value is/is't valid
	//Input parameter:
	//	row, col: Position will check
	//Output:
	//	true: Is valid
	//	false: not valid
	bool IsValid(int row, int col);

	///Function is calculate averager of height value with size near 3x3 vertex height
	//Input parameter:
	//	row, col: Position will calculate
	//Output:
	//	Average value at this position
	GLfloat AveragerHeight3x3(int row, int col);

	///Function is calculate average of all data and update it into list data
	//Input parameter:
	//	Void
	//Output:
	//	Void
	void Filter3x3();
public:
	HeightField() : Cols(0), Rows(0), HeightData(0), m_fileSrcName(0){}

	///Load height data form file source
	//Input parameters:
	//	fileSrcName: source file name
	//	iRows: Number of data row
	//	iCols: Number of data column
	//	heightScale: Scale value of map height
	//Output: Status of load function
	//	-1: Can't open source file
	//	-2: Can't create data buffer
	//	-3: Can't read with color type
	//	0: Load success
	int LoadHeightData(char *fileSrcName, int iRows, int iCols, float heightScale);

	float getHeight(int row, int col);

	~HeightField();
};

#endif